;(function($){
	$.fn.posorder_dish_type = function(opts){
		var el = this;
		var opt = {
			ct_cb : [],
			bh : "",
			dataBack : function(){},
			closeBack : function(){},
			freshBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.initTypeListValue();
		this.bindEvent();
		el.find("#menuLoadTime").text(localStorage.getItem("menuLoadTime"))
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.find("#close-dialog,#modalBtnClose").click(function(){
				el.modal("hide");
				param.closeBack();
				el.empty();
			});
			
			el.find(".dishes_type_each").unbind("click").click(function(e){
				e.stopPropagation();
				var js = {index:($(this).data("index")+1),bh:$(this).data("bh")};
				param.dataBack(js);
				el.modal("hide");
				el.empty();
			});
			
			//数据更新
			el.find("#modalDataUpdate").click(function(){
				el.modal("hide");
				el.empty();
//				jf_getPosDict("POSMenuSearch",localStorage.getItem("posDep"));
				jf_getPosDict("all",localStorage.getItem("posDep"));
				$.posorder_dishes.param.page = 1;
				$.posorder_dishes.initDs();
			})
		},
		initHtml : function(){
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="modal-dialog" style="width:calc( 80% - 130px)">'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white bigger-150">&times;</span>'+
										'</button>'+
										'<span id="modal-h4" style="font-size:20px;font-weight:bold;">选择</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="max-height:420px;">'+
									'<div class="typeList" style="max-height:390px;overflow-y:auto;overflow-x:hidden;padding:5px 0;"></div>'+
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearifx">'+
										'<div class="pull-left">'+
											'<div class="btn-group">'+
												'<button id="modalDataUpdate" class="btn btn-info">'+
//													'<i class="ace-icon fa fa-times"></i>'+
													'菜谱数据'+
													'&nbsp;&nbsp;<span id="menuLoadTime" data-i18n="更新数据" class=""></span>'+
												'</button>'+
											'</div>'+
										'</div>'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnClose" class="btn btn-default">'+
													'<i class="ace-icon fa fa-times"></i>'+
													'关闭'+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			this.el.html(html);
		},
		initTypeListValue : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var ct_cb = param.ct_cb;
//			console.log(JSON.stringify(ct_cb))
			var w = el.find(".modal-header").width()
			$(ct_cb).each(function(key,value){
//				var classBh = "btn-light"
//				if(value.bh==param.bh){
//					classBh = "currentType btn-primary";
//				}
				var width = w-30
//				var eachWidth = (width/6-4)
				var height = (width/6-4)*2/5*2
//				var fontSize = 15;
//				var maxinclude=8//默认字体下最大可容纳字数
//				var ifsmaller=''
////				if((value.mc.length) >= maxinclude){
////					if((value.mc.length*fontSize-8)>eachWidth){
//					if(eachWidth<maxinclude*fontSize){
//						fontSize = Math.floor(eachWidth / value.mc.length)
//						ifsmaller=' besmall'
//					}
////				}
//				var lineheight=0
//				if(height>50){
//					lineheight=50-20
//				}else{
//					lineheight=height-20
//				}
				
				html=getTypeElement(value,param.bh,width,height,6,key)
//				var html=$('<button type="button" class="btn btn-app '+classBh+' typelist_each" data-index='+key+' data-bh="'+value.bh+'" style="font-size:'+fontSize+'px;width:'+eachWidth+'px;max-height:50px;height:'+height+'px;">'+
//								'<div class="type_content'+ifsmaller+'" style="width:100%;height:100%;line-height:'+lineheight+'px">'+value.mc+'</div>'+
//							'</button>')
				$(html).appendTo(el.find(".typeList"))
			})
		}
	}
	
	//获取菜品分类对象
	function getTypeElement(data,bh,width,height,twidthNum,index){
		var $type;
		var thisclass="btn-light"//默认样式
		if(data.bh==bh){
			thisclass="btn-primary"//选中样式
		}else{
			thisclass="btn-light"//默认样式
		}
		var fontsize=18//默认字体大小
		var width=width/twidthNum-6//菜别类型宽度
		var eachlength=data.mc.length//名称长度
		var maxinclude=0//最大容量
		var ifsmaller=""
		maxinclude=width/fontsize
		if($(window).width()>1024){
			fontsize=18
		}else{
			fontsize=15
		}
		if(eachlength>=maxinclude){//超出容量
			fontsize=width/eachlength
			ifsmaller=" besmall"
		}
		$type=$('<div><button type="button" class="btn btn-app '+thisclass+' dishes_type_each" data-bh="'+ data.bh +'" data-index="'+index+'">'+
						'<div class="type_content'+ifsmaller+'">'+ data.mc +'</div>'+
					'</button></div>')
		$type.find(".dishes_type_each").css("font-size",fontsize+"px")//设置字体大小
		$type.find(".dishes_type_each").css("width",width).css("min-width",width)//设置宽度
		var hhhh = (height/2-6)>50 ? 50 : ((height/2-6)<30 ? 30 : (height/2-6))
//		$type.find(".dishes_type_each").css("height",height/2-6).css("max-height",height/2-6)//设置高度
		$type.find(".dishes_type_each").css("height",hhhh)
		$type.find(".dishes_type_each").css("padding",0)
		$type.find(".dishes_type_each").css("text-align","center")
		$type.find(".type_content").css("line-height",hhhh+"px")
		$type.find(".type_content").css("width","100%").css("height","100%")
		return $type.html()
	}
	
})(jQuery);